package org.apache.james.mime4j.message;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.apache.james.mime4j.MimeException;
import org.apache.james.mime4j.field.ContentTypeField;
import org.apache.james.mime4j.util.CharsetUtil;
import org.apache.james.mime4j.util.MessageUtils;

public class Multipart
  implements Body
{
  private List bodyParts;
  private String epilogue;
  private Entity parent;
  private String preamble = "";
  private String subType;

  public Multipart(String paramString)
  {
    this.epilogue = "";
    this.bodyParts = new LinkedList();
    this.parent = null;
    this.subType = paramString;
  }

  public void addBodyPart(BodyPart paramBodyPart)
  {
    this.bodyParts.add(paramBodyPart);
    paramBodyPart.setParent(this.parent);
  }

  public void dispose()
  {
    Iterator localIterator = this.bodyParts.iterator();
    while (localIterator.hasNext())
      ((BodyPart)localIterator.next()).dispose();
  }

  public List getBodyParts()
  {
    return Collections.unmodifiableList(this.bodyParts);
  }

  public String getEpilogue()
  {
    return this.epilogue;
  }

  public Entity getParent()
  {
    return this.parent;
  }

  public String getPreamble()
  {
    return this.preamble;
  }

  public String getSubType()
  {
    return this.subType;
  }

  public void setBodyParts(List paramList)
  {
    this.bodyParts = paramList;
    Iterator localIterator = paramList.iterator();
    while (localIterator.hasNext())
      ((BodyPart)localIterator.next()).setParent(this.parent);
  }

  public void setEpilogue(String paramString)
  {
    this.epilogue = paramString;
  }

  public void setParent(Entity paramEntity)
  {
    this.parent = paramEntity;
    Iterator localIterator = this.bodyParts.iterator();
    while (localIterator.hasNext())
      ((BodyPart)localIterator.next()).setParent(paramEntity);
  }

  public void setPreamble(String paramString)
  {
    this.preamble = paramString;
  }

  public void setSubType(String paramString)
  {
    this.subType = paramString;
  }

  public void writeTo(OutputStream paramOutputStream, int paramInt)
    throws IOException, MimeException
  {
    Charset localCharset;
    BufferedWriter localBufferedWriter;
    ContentTypeField localContentTypeField = (ContentTypeField)getParent().getHeader().getField("Content-Type");
    if ((localContentTypeField == null) || (localContentTypeField.getBoundary() == null))
      throw new MimeException("Multipart boundary not specified");
    String str = localContentTypeField.getBoundary();
    if (paramInt == 3)
      if ((localContentTypeField != null) && (localContentTypeField.getCharset() != null))
        localCharset = CharsetUtil.getCharset(localContentTypeField.getCharset());
    while (true)
    {
      while (true)
      {
        localBufferedWriter = new BufferedWriter(new OutputStreamWriter(paramOutputStream, localCharset), 8192);
        List localList = getBodyParts();
        localBufferedWriter.write(getPreamble());
        localBufferedWriter.write("\r\n");
        int i = 0;
        while (true)
        {
          if (i >= localList.size())
            break label199;
          localBufferedWriter.write("--");
          localBufferedWriter.write(str);
          localBufferedWriter.write("\r\n");
          localBufferedWriter.flush();
          ((BodyPart)localList.get(i)).writeTo(paramOutputStream, paramInt);
          localBufferedWriter.write("\r\n");
          ++i;
        }
        localCharset = MessageUtils.ISO_8859_1;
      }
      localCharset = MessageUtils.DEFAULT_CHARSET;
    }
    label199: localBufferedWriter.write("--");
    localBufferedWriter.write(str);
    localBufferedWriter.write("--");
    localBufferedWriter.write("\r\n");
    localBufferedWriter.write(getEpilogue());
    localBufferedWriter.flush();
  }
}